
var province = require('../../province.js');
// var confirm = require('../../confirm/confirm.js');

var app = getApp(),
  fn = app.fn;

Page({
  data: {
    newShow: true,
    addId: '',
    addressName: '',
    addressPhone: '',
    addressDetailed: '',
    provinceId: '',
    cityId: '',
    areaId: '',
    postcodes: '000000',
    address: [],
    provinceArray: [],
    cityArray: [],
    countyArray: [],
    provinceIndex: '',
    cityIndex: '',
    countyIndex: '',
    provinceName: '',
    cityName: '',
    countyName: '',
    addType: 0,
 
    block: 0,
  },
  ProID: '',
  CityID: '',
  AreID: '',
  cityArrayDemo: [],
  countyArrayDemo: [],

  onLoad: function (options) {
    this.setData({
      uid: app.globalData.uid,
    })

    this.animation = wx.createAnimation({
      duration: 300,
      timingFunction: 'ease'
    });
   
    this.getInitData();


 
    this.setData({
      block: options.block,
    })
  },

  onPullDownRefresh: function () {
    this.getInitData(function () {
      wx.stopPullDownRefresh();
    });
  },

  getprovinceData: function () {
    var provinceArrayDemo = []
    for (var i = 0; i < province.provinceArray.length; ++i) {
      var item = province.provinceArray[i]
      provinceArrayDemo.push(item.name)
    }
    this.setData({
      provinceArray: provinceArrayDemo
    })
  },


  getInitData: function () {

    var that = this;
    // list
    fn.apiData({
      url: 'user.php',
      data: {
        uid: that.data.uid,
      },
      success: function (res) {
    
        if (res.data.code==0) {
       
          that.setData({ 'address': res.data.data.consignee_list });
      
 
          wx.hideToast();
        }
      }
    });
  },

  bindSelect: function (e) {
    var that = this;

    var address = that.data.address;
    var aid = e.currentTarget.dataset.id;
    var block = that.data.block;

    if (block == 1) {
   
      for (var i = 0; i < address.length; i++) {
        var addInfo = address[i];
        if (addInfo.addId == aid) {
       
          var pagesLength = getCurrentPages().length;
       
          var frontPage = getCurrentPages()[pagesLength - 2];
        
          frontPage.addressBlock(addInfo);
        }
      }
    
      wx.navigateBack({
        delta: 1
      })
    }
  },


  defaultClick: function (e) {
    var that = this;
    that.setData({
      addId: e.currentTarget.dataset.id
    });

    fn.apiData({
      url: 'selectAddress.php',
      data: {
        uid: that.data.uid,
        addId: that.data.addId
      },
      success: function (res) {

        wx.showToast({
          title: res.data.error_msg,
          icon: 'success',
          duration: 1000
        })
        if (res.data.success) {
          that.setData({ 'address': [] });
          that.getInitData();
        }
      }
    });
  },


  deleteClick: function (e) {
    var that = this;
    that.setData({
      addId: e.currentTarget.dataset.id
    });
    wx.showModal({
      title: '提示',
      content: '确定要删除该地址吗?',
      success: function (res) {
        if (res.confirm) {

        }
      }
    })

  },

  deleteData: function (e) {
    var that = this;
    fn.apiData({
      url: 'deleteAddress.php',
      data: {
        uid: that.data.uid,
        addId: that.data.addId
      },
      success: function (res) {

        wx.showToast({
          title: res.data.error_msg,
          icon: 'success',
          duration: 2000
        })
        if (res.data.success) {
          that.setData({ 'address': [] });
          that.getInitData();
        }
      }
    });
  },


  detailsClick: function (e) {

    var that = this;
    that.setData({
      'newShow': false,
      addId: e.currentTarget.dataset.id,
      addType: 1,
    });
    fn.apiData({
      url: 'addressDetail.php',
      data: {
        uid: that.data.uid,
        addId: that.data.addId
      },
      success: function (res) {
        if (res.data.success) {
          that.setData({

            addressName: res.data.result.name,
            addressPhone: res.data.result.tel,
            addressDetailed: res.data.result.address,
            provinceId: res.data.result.province,
            cityId: res.data.result.city,
            areaId: res.data.result.area,
            provinceName: res.data.result.provinceName,
            cityName: res.data.result.cityName,
            countyName: res.data.result.areaName,
            provinceIndex: '',
            cityIndex: '',
            countyIndex: '',
          });

          this.ProID = that.data.provinceId,
            this.CityID = that.data.cityId,
            this.AreID = that.data.areaId,

         
            wx.hideToast();

          var proIndex;
          for (var i = 0; i < province.provinceArray.length; ++i) {
            var item = province.provinceArray[i]
            if (item.ProID == that.data.provinceId) {
              proIndex = i;
              break
            }
          }

          this.cityArrayDemo = []
      
          for (var i = 0; i < province.cityArray.length; ++i) {
            var item = province.cityArray[i]
            if (item.ProID == that.data.provinceId) {
              this.cityArrayDemo.push(item.name)
            }
          }

          var citIndex;
          for (var i = 0; i < this.cityArrayDemo.length; ++i) {
            var item = this.cityArrayDemo[i]
            if (item == that.data.cityName) {
              citIndex = i;
              break
            }
          }

          this.countyArrayDemo = []
          for (var i = 0; i < province.countyArray.length; ++i) {
            var item = province.countyArray[i]
            if (item.CityID == that.data.cityId) {
              this.countyArrayDemo.push(item.DisName)
            }
          }

          var couIndex;
          for (var i = 0; i < this.countyArrayDemo.length; ++i) {
            var item = this.countyArrayDemo[i]
            if (item == that.data.countyName) {
              couIndex = i;
              break
            }
          }

          that.setData({
            provinceIndex: proIndex,
            cityIndex: citIndex,
            countyIndex: couIndex,
            cityArray: this.cityArrayDemo,
            countyArray: this.countyArrayDemo,
          });
        }
      }
    });

    this.animation.opacity(1).translateY(0).step();
  },


  newOpen: function () {

  
    this.setData({
      'newShow': false,
      addressName: '',
      addressPhone: '',
      addressDetailed: '',
      province: '',
      provinceIndex: '',
      cityIndex: '',
      countyIndex: '',
      provinceId: '',
      cityId: '',
      areaId: '',
      provinceName: '',
      cityName: '',
      countyName: '',
      addType: 0,
    });

    this.animation.opacity(1).translateY(0).step();

  },

 
  newClose: function () {

    this.animation.opacity(0).translateY(500).step();

    setTimeout(function () {
     
      this.setData({
        'newShow': true
      });
    }.bind(this), 300);
  },


  submitClick: function () {

    if (this.data.addType == 0) {
      this.addClick();
    } else {
      this.editorClick();
    }
  },


  editorClick: function () {
    var that = this;
    fn.apiData({
      url: 'eidtAddress.php',
      data: {
        uid: that.data.uid,
        addId: that.data.addId,
        name: that.data.addressName,
        tel: that.data.addressPhone,
        postCode: that.data.postcodes,
        province: that.data.provinceId,
        city: that.data.cityId,
        area: that.data.areaId,
        address: that.data.addressDetailed,
      },
      success: function (res) {

        wx.showToast({
          title: res.data.error_msg,
          icon: '',
          duration: 2000
        })

        if (res.data.success) {
          that.newClose();
          that.setData({ 'address': [] });
          that.getInitData();
        }
      }
    });
  },

 
  addClick: function () {
    var that = this;
    fn.apiData({
      url: 'addAddress.php',
      data: {
        uid: that.data.uid,
        name: that.data.addressName,
        tel: that.data.addressPhone,
        postCode: that.data.postcodes,
        province: that.data.provinceId,
        city: that.data.cityId,
        area: that.data.areaId,
        address: that.data.addressDetailed,
      },
      success: function (res) {

        wx.showToast({
          title: res.data.error_msg,
          icon: '',
          duration: 2000
        })

        if (res.data.success) {
          that.newClose();
          that.setData({ 'address': [] });
          that.getInitData();
        }
      }
    });
  },



  bindName: function (e) {
    this.setData({
      addressName: e.detail.value
    })
  },

  bindPhone: function (e) {
    this.setData({
      addressPhone: e.detail.value
    })
  },
  bindDetailed: function (e) {
    this.setData({
      addressDetailed: e.detail.value
    })
  },


  bindProvinceChange: function (e) {
   
    var name;
    for (var i = 0; i < province.provinceArray.length; ++i) {
      var item = province.provinceArray[i]
      if (i == e.detail.value) {
        this.ProID = item.ProID,
          name = item.name
        break
      }
    }
    this.cityArrayDemo = []

    for (var i = 0; i < province.cityArray.length; ++i) {
      var item = province.cityArray[i]
      if (item.ProID == this.ProID) {
        this.cityArrayDemo.push(item.name)
      }
    }

    if (this.data.provinceId != this.ProID) {

      this.setData({
        provinceId: this.ProID,
        provinceName: name,
        provinceIndex: e.detail.value,
        cityArray: this.cityArrayDemo,
        cityIndex: '',
        countyIndex: '',
        cityId: '',
        areaId: '',
        cityName: '',
        countyName: '',
      })
    } else {

      this.setData({
        provinceId: this.ProID,
        provinceName: name,
        provinceIndex: e.detail.value,
        cityArray: this.cityArrayDemo,
      })

    }
  },

  bindCityChange: function (e) {
 
    var name;
    for (var j = 0; j < province.cityArray.length; ++j) {
      var item = province.cityArray[j]
      if (item.name == this.data.cityArray[e.detail.value]) {
        this.CityID = item.CityID,
          name = item.name
        break
      }
    }

    this.countyArrayDemo = []
    for (var i = 0; i < province.countyArray.length; ++i) {
      var item = province.countyArray[i]
      if (item.CityID == this.CityID) {
        this.countyArrayDemo.push(item.DisName)
      }
    }

    if (this.data.cityId != this.CityID) {
      this.setData({
        cityId: this.CityID,
        cityIndex: e.detail.value,
        cityName: name,
        countyArray: this.countyArrayDemo,
        countyIndex: '',
        areaId: '',
        countyName: '',
      })

    } else {
      this.setData({
        cityId: this.CityID,
        cityIndex: e.detail.value,
        cityName: name,
        countyArray: this.countyArrayDemo,
      })

    }



  },

  bindCountyChange: function (e) {
    var name;

    for (var i = 0; i < province.countyArray.length; ++i) {
      var item = province.countyArray[i]
      if (item.DisName == this.data.countyArray[e.detail.value]) {
        this.AreID = item.Id,
          name = item.DisName
        break
      }
    }

    this.setData({
      areaId: this.AreID,
      countyIndex: e.detail.value,
      countyName: name,
    })
  },



  onReady: function () {

  },
  onShow: function () {
 
  },
  onHide: function () {
  
  },
  onUnload: function () {
  
  }
})